package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class YetAdapter extends RecyclerView.Adapter<YetAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<StoreInfo.DataBean> mList;
    private SimpleDialog selfDialog;
    private int i = 0;

    public YetAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_yet_recycler, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.shop_name.setText(mList.get(position).getName());
        Glide.with(mContext).load(mList.get(position).getImg_url()).into(holder.shop_icon);
        holder.shop_tel.setText(mList.get(position).getTel());
        holder.shop_user.setText(mList.get(position).getContact());
        holder.shop_location.setText(mList.get(position).getSpecific_address());
        holder.shop_sun.setText(mList.get(position).getAll_num());
        //判断该店铺下有无密码设备
        if (mList.get(position).getMima_num().equals("0")) {
            holder.shop_mima.setVisibility(View.GONE);
        }else {
            holder.shop_mima.setVisibility(View.VISIBLE);
            holder.shop_mima.setText(mList.get(position).getMima_num());
        }

        //判断该店铺下有无wifi设备
        if (mList.get(position).getOnline_num().equals("0")) {
            holder.shop_number.setVisibility(View.GONE);
        }else {
            holder.shop_number.setVisibility(View.VISIBLE);
            holder.shop_number.setText(mList.get(position).getOnline_num());
        }
        if (mList.get(position).getOffine_num().equals("0")) {
            holder.shop_lx_wifi.setVisibility(View.GONE);
        }else {
            holder.shop_lx_wifi.setVisibility(View.VISIBLE);
            holder.shop_lx_wifi.setText(mList.get(position).getOffine_num());
        }


        
        //判断该店铺下有无纸巾机
        if (mList.get(position).getPaper_online() == 0) {
            holder.shop_paper.setVisibility(View.GONE);
        }else{
            holder.shop_paper.setVisibility(View.VISIBLE);
            holder.shop_paper.setText(mList.get(position).getPaper_online()+"");
        }

        if (mList.get(position).getPaper_offline() == 0) {
            holder.shop_paper_lx.setVisibility(View.GONE);
        }else {
            holder.shop_paper_lx.setVisibility(View.VISIBLE);
            holder.shop_paper_lx.setText(mList.get(position).getPaper_offline()+"");
        }


        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position), position);
        });

        //holder.shop_zhang.setText(mList.get(position).getSub_count()+" (今)");
        holder.shop_tel.setOnClickListener(v -> {
            Dialog(position);

        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView shop_icon;
        TextView shop_paper_lx,shop_name,shop_user,shop_location,shop_tel,shop_sun,shop_number,shop_lx_wifi,shop_mima,shop_zhang,shop_paper;

        public ViewHolder(View itemView) {
            super(itemView);
            shop_icon = (ImageView) itemView.findViewById(R.id.item_icon);
            shop_name = (TextView) itemView.findViewById(R.id.item_name);
            shop_user = (TextView) itemView.findViewById(R.id.shop_user);
            shop_location = (TextView) itemView.findViewById(R.id.shop_location);
            shop_tel = (TextView) itemView.findViewById(R.id.shop_tel);
            shop_number = (TextView) itemView.findViewById(R.id.shop_number);
            shop_sun = (TextView) itemView.findViewById(R.id.shop_sun);
            shop_lx_wifi = (TextView) itemView.findViewById(R.id.shop_lx_wifi);
            shop_mima = (TextView) itemView.findViewById(R.id.shop_mima);
            shop_zhang = (TextView) itemView.findViewById(R.id.shop_zhang);
            shop_paper_lx = (TextView) itemView.findViewById(R.id.shop_paper_lx);
            shop_paper = (TextView) itemView.findViewById(R.id.shop_paper);
        }
    }

    private void Dialog(int posotion) {
        selfDialog = new SimpleDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage(mList.get(posotion).getTel());
        selfDialog.setYesOnclickListener("确定", () -> {
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mList.get(posotion).getTel()));
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mContext.startActivity(intent);
            selfDialog.dismiss();
        });
        selfDialog.setNoOnclickListener("取消", () -> {
            selfDialog.dismiss();
        });
        selfDialog.show();
    }



    public void addData(List<StoreInfo.DataBean> List){
        if (mList != null) {
            mList.clear();
            mList.addAll(List);
            notifyDataSetChanged();
        }
    }

    public void addLastData(List<StoreInfo.DataBean> data){
        mList.addAll(data);
        notifyDataSetChanged();
    }


    public interface OnItemClickListenter {
        void itemClickListenter(StoreInfo.DataBean dataBean,int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

    public void deleteItem(int position){
        if (mList != null) {
            mList.remove(position);
            notifyDataSetChanged();
        }
    }
}
